const Stock = require("../../modules/Stock")
const $asyncForeach = require("../../utils/asyncForeach")
const $request = require("../../utils/request")

let isScaning = false

module.exports = async () => {

    if (!isScaning) {
        isScaning = true

        let individualList = await Stock.getIndividualList()
        let recommendList = []

        setTimeout(async () => {

            try {
                await $asyncForeach({
                    list: individualList, fn: async ({ code }, index) => {

                        let stock = new Stock({ code, type: "stock" })
                        let transaction = await stock.getTransaction({ ktype: "day" })
                        let details = await stock.generateDetails({ transaction })
                        let detailsLatest = Stock.getDetailsLatest({ details })

                        let divergence = Stock.divergence({ details: detailsLatest })
                        let latestKd = Stock.latestKd({ details: detailsLatest })

                        /**
                         * 判断日线是否底背离
                         */
                        if (
                            divergence.bottom
                            && divergence.bottom.range.length > 20
                            && divergence.bottom.ago < 3
                            && latestKd.k < 14
                        ) {
                            /**
                             * 判断下个月线是否还是向上
                             */
                            let transactionMonth = await stock.getTransaction({ ktype: "month" })
                            let detailsMonth = await stock.generateDetails({ transaction: transactionMonth })
                            let detailsLatestMonth = Stock.getDetailsLatest({ details: detailsMonth })
                            let { ma5 } = detailsLatestMonth.transaction.at(-1)

                            // 加入一条新的K线
                            let pushTransactionMonth = Stock.pushTransaction({ transaction: transactionMonth, pushTransactionMonth: { open: -1, close: -1, high: -1, low: -1 } })
                            let newDetailsMonth = await stock.generateDetails({ transaction: pushTransactionMonth })
                            let { ma5: newMa5 } = Stock.getDetailsLatest({ details: newDetailsMonth }).transaction.at(-1)

                            console.log({ newMa5, ma5 })

                            if (newMa5 > ma5) {
                                let latestMaDifference = Stock.latestMaDifference({ details: detailsLatest })
                                detailsLatest.analysis = { divergence, latestKd, latestMaDifference }

                                recommendList.push(detailsLatest)
                            }
                        }

                        console.log(`${index + 1}/${individualList.length} ${code} listLength: ${recommendList.length}`)
                    }
                })
            }
            catch (e) {}

            await $request({
                url: "https://cong.yuyoupx.com/api/stock/recommend/save",
                method: "POST",
                data: {
                    recommendList: JSON.stringify(recommendList)
                }
            })

            isScaning = false

        })
    }

}